package com.weixing.mall.provider.controller.rpc;

import cn.hutool.core.util.ObjectUtil;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.provider.api.model.vo.ProductStockDetailsVo;
import com.weixing.mall.provider.api.model.vo.ProductStockVo;
import com.weixing.mall.provider.api.model.vo.dto.ErpSyncDto;
import com.weixing.mall.provider.model.domain.ProductStockDetails;
import com.weixing.mall.provider.service.IProductStockDetailsService;
import com.weixing.mall.provider.service.IProductStockReportService;
import com.weixing.mall.provider.service.IProductStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wx
 * @Date 2019/12/6 16:10
 * @Version 1.0
 */
@RestController
@RequestMapping("api/productStock")
public class ProductStockRpcController {
    @Autowired
    private IProductStockService productStockService;
    @Autowired
    private IProductStockReportService productStockReportService;
    @Autowired
    private IProductStockDetailsService productStockDetailsService;
    //根据商品信息和仓库Id查询 返回Id
    @GetMapping("getByGoodsInfo")
    public Result<Integer> getByGoodsInfo(Long depotId,Long goodsId,Long productId){
        return ResultUtil.success(productStockService.getByGoodsInfo(depotId,goodsId,productId));
    }
/*
* 系统内置的出入库
* depotId出入库的Id号，productId 商品ID， flag 为2代表出库，flag为1代表入库。uid 系统当前操作人员
*
* */
    @GetMapping("inOutStock")
    public Result inOutStock(Long depotId,Long productId,Integer flag,Long uid,String stockSn){
        productStockService.inOutStock(depotId,productId,flag,uid,stockSn);
        return ResultUtil.success();
    }

    @GetMapping("getAllByDepotId")
    public List<ProductStockVo> getAllByDepotId(Long depotId){
        return productStockService.getAllAvaByDepotId(depotId);
    }

    /**
     * 项目二期，根据库存ID获取库存详情
     * @param depotId
     * @return
     */
    @GetMapping("getAllByDepotId2")
    public List<ProductStockDetailsVo> getAllByDepotId2(Long depotId){
        return productStockDetailsService.getAllAvaByDepotId(depotId);
    }
    /**
     * 项目二期，锁定库存
     * @param id 库存id
     * @return
     */
    @GetMapping("lockDepot")
    public Result lockDepot(Long id){
        ProductStockDetails vo=new ProductStockDetails();
        vo.setId(id);
        vo.setIsLock(WhetherEnum.YES.getValue());
        productStockDetailsService.updateById(vo);
        return ResultUtil.success();
    }
    /**
     * 用于将库存数据同步到库存统计表中（wms_product_stock_report）
     * @return
     */
    @GetMapping("updateSynData")
    public Result updateSynData(String yearMonth){
        productStockReportService.updateSynData(yearMonth);
        return ResultUtil.success();
    }
    /**
     * 用于同步前删除数据
     * @return
     */
    @GetMapping("delSyncErpData")
    public Result delSyncErpData(Long depotId){
        productStockService.delSyncErpData(depotId);
        return ResultUtil.success();
    }
    /**
     * 用于将ERP商品库存数据同步到库存中（ERP同步专用）
     * @return
     */
    @PostMapping("erpSynData")
    public Result erpSynData(@RequestBody ErpSyncDto erpSyncDto){
        if(ObjectUtil.isNotEmpty(erpSyncDto)){
            productStockService.syncErpProductStock(erpSyncDto.getList());
        }
        return ResultUtil.success();
    }

}
